﻿using System;
using Shell.PocoDot;

namespace Shell.DomainDot
{
    public class WithOne<TPocoDot> : IDisposable
        where TPocoDot : PocoDotBase
    {
        private object syncroot;
        private bool disposed;
        private bool isDirty;

        public WithOne(Guid key, IDataContext<TPocoDot> repository,
            IRepository relationRepository)
        {
            this.syncroot = new object();
            this.isDirty = true;
            this.MainKey = key;
            this.Repository = repository;
            this.RelationRepository = relationRepository;
        }

        protected TPocoDot LazyInstance { get; set; }

        protected Guid MainKey { get; private set; }

        protected IDataContext<TPocoDot> Repository { get; private set; }

        protected IRepository RelationRepository { get; private set; }

        public virtual TPocoDot Instance
        {
            get
            {
                if (this.isDirty)
                {
                    lock (this.syncroot)
                    {
                        if (this.isDirty)
                        {
                            this.Load();
                        }
                    }
                }
                return this.LazyInstance;
            }
        }

        public virtual TPocoDot Create()
        {
            if (this.Instance != null)
            {
                throw new InvalidOperationException("Instance does exist, could not re-create it.");
            }
            try
            {
                var created = this.Repository.Create();
                this.Bind(created.Id);
                return created;
            }
            catch (Exception) //TODO
            {
                throw;
            }
        }

        public virtual void Delete()
        {
            if (this.Instance == null)
            {
                throw new InvalidOperationException("Instance dose not exist, could not delete it before created.");
            }
            try
            {
                this.Repository.Delete(this.Instance.Id);
                this.Release();
            }
            catch (Exception) //TODO
            {
                throw;
            }
        }

        public virtual void Bind(Guid relatedKey)
        {
            try
            {
                this.RelationRepository.Bind(this.MainKey, relatedKey);
                this.isDirty = true;
            }
            catch (Exception) //TODO
            {
                throw;
            }
        }

        public virtual void Release()
        {
            if (this.Instance == null)
            {
                throw new InvalidOperationException("Instance dose not exist, could not release it before created.");
            }
            try
            {
                this.RelationRepository.Release(this.MainKey, this.Instance.Id);
                this.isDirty = true;
            }
            catch (Exception) //TODO
            {
                throw;
            }
        }

        public int Submit()
        {
            try
            {
                var count1 = this.Repository.Submit();
                var count2 = this.RelationRepository.Submit();
                return count1 + count2;
            }
            catch (Exception) //TODO
            {
                throw;
            }
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!this.disposed)
                {
                    this.Repository = null;
                    this.RelationRepository = null;
                }
                this.disposed = true;
            }
        }

        protected void Load()
        {
            this.OnLoad();
            this.isDirty = false;
        }

        protected virtual void OnLoad()
        {
            Guid relatedKey;
            if (this.RelationRepository.TryRead(this.MainKey, out relatedKey))
            {
                this.LazyInstance = this.Repository.Read(relatedKey);
            }
        }
    }
}
